/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vn.support.ui.frame;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.swing.table.AbstractTableModel;
import vn.support.core.dto.Visitor;
import vn.support.ui.View;

/**
 *
 * @author nttuyen
 */
public class TableClientOnlineModel extends AbstractTableModel {

    private final String[] columns;
    private final List<Visitor> visitors;
    private final Map<String, Integer> visitorIndex;
    protected ResourceBundle bundle;
    
    public TableClientOnlineModel() {
        this.bundle = ResourceBundle.getBundle(View.UI_BUNDLE);
        this.columns = new String[] {
            bundle("ui.contact.visitor.table.chat.id"),
            bundle("ui.contact.visitor.table.chat.status"),
            bundle("ui.contact.visitor.table.chat.department"),
            bundle("ui.contact.visitor.table.chat.operator"),
            //bundle("ui.contact.visitor.table.chat.totaltime"),
            bundle("ui.contact.visitor.table.chat.waittime"),
            bundle("ui.contact.visitor.table.chat.currentpage"),
            //bundle("ui.contact.visitor.table.chat.country")
        };
        this.visitors = new LinkedList<Visitor>();
        this.visitorIndex = new HashMap<String, Integer>();
    }

    protected final String bundle(String key) {
        if(this.bundle.containsKey(key)) {
            return this.bundle.getString(key);
        }
        return key;
    }

    @Override
    public int getRowCount() {
        return visitors.size();
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public String getColumnName(int column) {
        return this.columns[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Visitor visitor = this.visitors.get(rowIndex);
        if(visitor == null) {
            return "";
        }
        switch(columnIndex) {
            case 0:
                   return visitor.getChatterId();
            case 1:
                return visitor.getState();
            case 2:
                return visitor.getDepartment();
            case 3:
                return visitor.getChatStaffName();
//            case 4:
//                return "";
            case 4:
                return visitor.getWaitTime();
            case 5:
                return visitor.getCurrentPage();
//            case 7:
//                return visitor.getCountry();
            default:
                return "";
        }
    }

    public void refresh(final List<Visitor> values) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                for(Visitor visitor : visitors) {
                    boolean exists = false;
                    for(Visitor v : values) {
                        if(v.getSessionId().equals(visitor.getSessionId())) {
                            exists = true;
                            break;
                        }
                    }
                    if(!exists) {
                        visitors.remove(visitor);
                        fireTableRowsDeleted(i, i);
                    } else {
                        i++;
                    }
                }
                //Update map
                visitorIndex.clear();
                int size = visitors.size();
                for(i = 0; i < size; i++) {
                    Visitor visitor = visitors.get(i);
                    visitorIndex.put(visitor.getSessionId(), i);
                }
                for(Visitor v : values) {
                    add(v);
                }
            }
        }).start();
    }

    public void add(Visitor visitor) {
        int size = this.visitors.size();
        String session = visitor.getSessionId();
        if(visitorIndex.containsKey(session)) {
            int index = this.visitorIndex.get(session);
            this.visitors.set(index, visitor);
            this.fireTableRowsUpdated(index, index);
        } else {
            this.visitors.add(visitor);
            this.visitorIndex.put(session, size);
            this.fireTableRowsInserted(size, size);
        }
    }

    public Visitor getVisitor(int index) {
        return this.visitors.get(index);
    }
}
